using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Fanfou
{
    /// <summary>
    /// Request Method
    /// </summary>
    public enum FanfouRequestMethod
    {
        /// <summary>
        /// Default method of the HTTP Request.
        /// </summary>
        GET = 0,

        /// <summary>
        /// Use POST when you need post some data to fanfou.com
        /// </summary>
        POST = 1
    }

    /// <summary>
    /// The HTTP Request of API invoking.
    /// </summary>
    public class FanfouRequest
    {
        private FanfouSession _session;
        private const string _postContentType = "application/x-www-form-urlencoded";
        private bool _requireAuthenticate;
        private FanfouRequestMethod _requestMethod;
        private HttpWebRequest _httpRequest;

        private const string _requestUrlPrefix = "http://api.fanfou.com/";

        private string _url;

        /// <summary>
        /// The request method.
        /// </summary>
        public FanfouRequestMethod RequestMethod
        {
            get { return _requestMethod; }
            set { _requestMethod = value; }
        }

        /// <summary>
        /// prepare a request.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        public FanfouRequest(string url, FanfouRequestMethod method)
        {
            _url = url;
            _requestMethod = method;
        }

        /// <summary>
        /// Perform a request that need authentication.
        /// </summary>
        /// <param name="url">the url requested.</param>
        /// <param name="method">the method</param>
        /// <param name="session">session</param>
        public FanfouRequest(string url, FanfouRequestMethod method, FanfouSession session)
        {
            _url = url;
            _requestMethod = method;
            _session = session;
        }

        /// <summary>
        /// Send the prepared FanfouRequest.
        /// </summary>
        /// <returns></returns>
        public FanfouResponse Send()
        {
            FanfouResponse response = new FanfouResponse();

            _httpRequest = (HttpWebRequest)WebRequest.Create(_url);
            if (_requestMethod == FanfouRequestMethod.POST)
            {
                _httpRequest.Method = "POST";
                _httpRequest.ContentType = _postContentType;
                using (Stream stream = _httpRequest.GetRequestStream())
                {
                }
            }

            response.Response = _httpRequest.GetResponse();

            return response;
        }
    }
}
